/**
 * Beaver - Zend/Ext.Direct framework
 *
 * @license     http://www.gnu.org/licenses/gpl-2.0.txt
 * @author      Mihály Kuprivecz <mkuprivecz@gmail.com>
 * @namespace   Beaver.Modules.User
 */

Ext.ns('Beaver.Modules.Partner');

Beaver.Modules.Partner.Store = Ext.extend( Beaver.Modules.General.Store, {
	constructor: function(config) {
		config        = config || {};
		config.fields = config.fields || [];
		config.controllerName = 'Partner';

		Ext.applyIf( config.fields, [
			{ name: 'id', mapping: 'id' },
			{ name: 'name', mapping: 'name', allowBlank: false },
			{ name: 'description', mapping: 'description', allowBlank: false },
			{ name: 'web', mapping: 'web', allowBlank: true },
			{ name: 'email', mapping: 'email', allowBlank: true },
			{ name: 'city', mapping: 'city', allowBlank: true, type: 'int' },
			{ name: 'postal_code', mapping: 'postal_code', allowBlank: true },
			{ name: 'address', mapping: 'address', allowBlank: true },
			{ name: 'phone', mapping: 'phone', allowBlank: true },
			{ name: 'contact_name', mapping: 'contact_name', allowBlank: true },
			{ name: 'contact_phone', mapping: 'contact_phone', allowBlank: true },
			{ name: 'contact_email', mapping: 'contact_email', allowBlank: true }
		]);
		
		Beaver.Modules.Partner.Store.superclass.constructor.apply(this, [config]);
	}
});

Ext.reg('beaverpartner', Beaver.Modules.Partner.Store);


Beaver.Modules.Partner.Grid = Ext.extend(Beaver.Modules.General.Grid, {
	initComponent: function() {
		var store  = new Beaver.Modules.Partner.Store();
		/*var cityStore = new Beaver.Modules.City.Store({
			autoLoad: {
				params:{
					sort: 'name',
					dir: 'ASC'
				}
			}
		});*/
		var cityStore = Ext.StoreMgr.lookup('cityStore');
		var editor = new Beaver.Modules.General.RowEditor()
		editor.on('canceledit', function() {
			if(store.getAt(0).phantom)
			{
				store.removeAt(0);
			}
		});
		
		var cityComboBox = new Ext.form.ComboBox({
			typeAhead: true,
			editable: true,
			//pageSize: 20,
			queryParam: 'filter[][name][string][right]',
			allowBlank: false,
			mode: 'local',
			store : cityStore,
			valueField: 'id',
			displayField: 'name'
		});
		
		var self  = this;
		var config = {
			store: store,
			plugins: [ editor ],
			columns: [
				{
					header: 'id',
					sortable: true,
					dataIndex: 'id',
					width: 40
				},
				{
					header: 'Név',
					sortable: true,
					dataIndex: 'name',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Leírás',
					sortable: true,
					dataIndex: 'description',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Web',
					sortable: true,
					dataIndex: 'web',
					editor: new Ext.form.TextField({vtype:'url'})
				},
				{
					header: 'E-mail',
					sortable: true,
					dataIndex: 'email',
					editor: new Ext.form.TextField({vtype:'email'})
				},
				{
					header: 'Település',
					sortable: true,
					dataIndex: 'city',
					width: 120,
					renderer: function(value, metaData, record, rowIndex, colIndex, store) {
						return cityStore.getAt(cityStore.find("id",value)).data.name;
					},
					editor: cityComboBox
				},
				{
					header: 'Irsz',
					sortable: true,
					dataIndex: 'postal_code',
					editor: new Ext.form.TextField(),
					width: 40
				},
				{
					header: 'Cím',
					sortable: true,
					dataIndex: 'address',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Telefon',
					sortable: true,
					dataIndex: 'phone',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Kontakt név',
					sortable: true,
					dataIndex: 'contact_name',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Kontakt telefon',
					sortable: true,
					dataIndex: 'contact_phone',
					editor: new Ext.form.TextField()
				},
				{
					header: 'Kontakt e-mail',
					sortable: true,
					dataIndex: 'contact_email',
					editor: new Ext.form.TextField({vtype:'email'})
				}

			],
			tbar: [
				{
					text: 'Partner hozzáadása',
					handler: function(){
						if(self.store.getAt(0) && self.store.getAt(0).phantom)
						{
							self.store.removeAt(0);
						}
						
						var record = new store.recordType({
							name : '',
							city : 1
						});

						editor.stopEditing();
						self.store.insert(0, record);
						editor.startEditing(0);
					}
				},
				{
					text: 'Kijelöltek törlése',
					handler: function(){
						
						editor.stopEditing();
						var records = self.getSelectionModel().getSelections();
						if (records.length > 0)
						{
					
							Ext.Msg.confirm('Törélés', 'Biztosan törölni szeretné az összes kijelölt elemet?', function( answer ){
								if (answer == "yes")
								{
									self.getStore().remove(records);
								}
				        	},this);
						}
					}
				}
			],
			bbar: new Ext.PagingToolbar({
				store: store,
				pageSize:500,
				displayInfo:true
			})


		};
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		Beaver.Modules.Partner.Grid.superclass.initComponent.apply(this, arguments);
	},
	onRender:function() {
		Beaver.Modules.Partner.Grid.superclass.onRender.apply(this, arguments);
	}
});

Ext.reg('beavergridpartner', Beaver.Modules.Partner.Grid);